<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// [ 应用入口文件 ]
namespace think;
use app\common\controller\Task;
use Swoole\Timer;
use think\facade\Log;

require __DIR__ . '/vendor/autoload.php';
$app = new App();
$http = $app->http;
$http->run();
//ini_set('memory_limit', '6M');
// 执行HTTP应用并响应

$data = [
    //高频任务
    'frequent' => [
        [
            'interval' => 5,
            'name' => 'PlatformTask',
            'description' => '区域任务'
        ],
        [
            'interval' => 5,
            'name' => 'UpdateTaskStatus',
            'description' => '更新测评任务状态-教职工'
        ],
        [
            'interval' => 5,
            'name' => 'UpdateWorkersTaskStatus',
            'description' => '更新测评任务状态-教职工'
        ],
        [
            'interval' => 5,
            'name' => 'graduate',
            'description' => ' 学院制【到期自动升级】'
        ],
        [
            'interval' => 5,
            'name' => 'groupActivityTask',
            'description' => '团体活动'
        ],
        [
            'interval' => 5,
            'name' => 'trainingTask',
            'description' => '培训讲座'
        ],
        [
            'interval' => 5,
            'name' => 'consultOffline',
            'description' => '线下预约更新状态'
        ],
        [
            'interval' => 5,
            'name' => 'questionnaireTask',
            'description' => '问卷'
        ],
        // 其他高频任务...
    ],
    //低频任务
    'infrequent' => [
        [
            'interval' => 43200,
            'name' => 'crisisRemind',
            'description' => '危机干预-提醒消息'
        ],
//        [
//            'interval' => 43200,
//            'name' => 'remind',
//            'description' => '提醒消息'
//        ]
    ]
];

$Task = new Task($app);
//高频任务
Timer::tick(5000, function() use ($Task, $data) {
    try {
        $Task->runTask($data['frequent']);
    } catch (\Throwable $e) {
        // 实现日志记录逻辑
        Log::channel('task')->error('高频任务执行失败: '.$e->getMessage());
    } finally {
        // 手动触发垃圾回收
        gc_collect_cycles();
    }
});
//低频任务
Timer::tick(43200000, function() use ($Task, $data) {
    try {
        $Task->runTask($data['infrequent']);
    } catch (\Throwable $e) {
        // 实现日志记录逻辑
        Log::channel('task')->error('低频任务执行失败: '.$e->getMessage());
    } finally {
        // 手动触发垃圾回收
        gc_collect_cycles();
    }
});

//保持脚本运行
\Swoole\Event::wait();
